$().ready(function() {

var link = $('#awplus_content a[href=#all]');
var html = '<input id="ap_bt_fleetparcel" type="button" value="Qty" class="smbutton"/>';

var items = ['<div class="ap_fleetparcel_item" style="padding: 2px; cursor: pointer;">All</div>',
             '<div class="ap_fleetparcel_item" style="padding: 2px; cursor: pointer;">1/2</div>',
             '<div class="ap_fleetparcel_item" style="padding: 2px; cursor: pointer;">1/3</div>',
             '<div class="ap_fleetparcel_item" style="padding: 2px; cursor: pointer;">1/4</div>',
             '<div class="ap_fleetparcel_item" style="padding: 2px; cursor: pointer;">1/5</div>'];
             
html += '<span id="ap_fleetparcel_box">'+ items.join('') +'</span>';

link.parent('td').empty().html(html);

$('#ap_fleetparcel_box').hide(0);

$('#ap_bt_fleetparcel').click(function() {
  var pos = $('#ap_bt_fleetparcel').offset();
  $('#ap_fleetparcel_box')
    .css({
      'position': 'absolute',
      'top': pos.top + $('#ap_bt_fleetparcel').height() + 2,
      'left': pos.left,
      'background': '#404040',
      'padding': '2px',
      'border': '1px solid #000000',
      'width': '40px'
    })
    .toggle();
});

// Load total fleets
var fleets = [];
$('#awplus_content tr:gt(1):lt(5)').each(function() {
  var tt = $.trim($('td:eq(2)', this).text());
  fleets.push(parseInt(tt));
});

$('.ap_fleetparcel_item')
  .click(function() {   
    var parcels_data = {'All': 1, '1/2': 2, '1/3': 3, '1/4': 4, '1/5': 5};
    var parcel = parcels_data[ $.trim($(this).text()) ];
    $('#awplus_content tr:gt(1):lt(5) input:text').each(function(i) {
      $(this).val( Math.floor(fleets[i] / parcel) );
    });
    
    $('#ap_fleetparcel_box').hide();
  })
  .hover(function() { $(this).css('background-color','#206060') },
         function() { $(this).css('background-color','#404040') });

});
